package com.clz.hyperit.controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.clz.hyperit.entity.Answer;
import com.clz.hyperit.entity.Article;
import com.clz.hyperit.entity.Question;
import com.clz.hyperit.entity.Tag;
import com.clz.hyperit.entity.UserDetail;
import com.clz.hyperit.service.PositionService;
import com.clz.hyperit.service.QuestionService;
import com.clz.hyperit.service.TagService;
import com.clz.hyperit.service.UserDetailService;
import com.clz.hyperit.util.ResultMessage;
import com.clz.hyperit.vo.Answer_User;
import com.clz.hyperit.vo.PageInfo;
import com.clz.hyperit.vo.Question_User;

@RestController
@RequestMapping(value = "/question")
public class QuestionController {
	private ResultMessage result = null;
	private final Logger logger = LoggerFactory
			.getLogger(PositionController.class);
	@Autowired
	private QuestionService questionService;

	@Autowired
	private TagService tagService;
	@Autowired
	private UserDetailService userDetailService;

	/**
	 * 发表问题
	 * 
	 * @param question
	 * @param tag
	 * @return
	 */
	@RequestMapping(value = "/saveQuestion", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> saveQuestion(Question question,
			String tag) {
		result = new ResultMessage();
		result.setServiceResult(true);
		Integer question_id = questionService.saveQuestion(question, tag);
		if (tag != null) {
			tagService.saveTagQuestion(question_id, tag);
		}
		result.setResultInfo("提問成功");
		result.getResultParm().put("article_id", question_id);
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 * 获取一个问题
	 */
	@RequestMapping(value = "/getOneQuestion", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> getOneQuestion(Integer question_id,
			Integer userdetail_id) { // userdetail_id用来判断是否本人查看问题
		result = new ResultMessage();
		result.setServiceResult(true);
		Question question = questionService.findOneQuestion(question_id);
		if (question == null) {
			result.setServiceResult(false);
			result.setResultInfo("问题不存在");
		} else {
			// article.setContent_md(null);
			// 获取作者id
			UserDetail ud = userDetailService.getUserInfo(question
					.getUserDetail_id());
			if (ud.getId() != userdetail_id) {
				question.setReading(question.getReading() + 1);
				questionService.updateQuestion(question);
				question = questionService.findOneQuestion(question_id);
			}
			List<Tag> tag_list = tagService.findTagByQuestion(question.getId());
			result.getResultParm().put("user", ud);
			result.getResultParm().put("question", question);
			result.getResultParm().put("tag", tag_list);
		}
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 * 分页查询所有问题-时间排序
	 */
	@RequestMapping(value = "/getQuestion", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> getQuestion(PageInfo pageInfo) {
		result = new ResultMessage();
		result.setServiceResult(true);
		List<Question_User> question_user_list = null;
		question_user_list = questionService.findAll(pageInfo);
		if (question_user_list.size() <= 0) {
			result.setServiceResult(false);
			result.setResultInfo("没有更多了");
		} else {
			result.setResultInfo("问题获取成功");
		}
		result.getResultParm().put("question_user", question_user_list);
		result.getResultParm().put("total_count", pageInfo.getTotalCount());
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 * 获取某人的所有问题
	 */
	@RequestMapping(value = "/getPersonQuestion", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> getPersonQuestion(PageInfo pageInfo,
			Integer userdetail_id) {
		result = new ResultMessage();
		result.setServiceResult(true);
		List<Question_User> question_user_list = questionService
				.findPersonQuestion(pageInfo, userdetail_id);
		if (question_user_list.size() <= 0) {
			result.setServiceResult(false);
			result.setResultInfo("没有更多了");
		} else {
			result.setResultInfo("问题获取成功");
		}
		result.getResultParm().put("question_user_list", question_user_list);
		result.getResultParm().put("total_count", pageInfo.getTotalCount());
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 * 删除问题
	 */
	@RequestMapping(value = "/deleteQuestion", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> deleteQuestion(Integer userdetail_id,
			Integer question_id) {
		result = new ResultMessage();
		result.setServiceResult(true);
		Question question = questionService.findOneQuestion(question_id);
		if (question.getUserDetail_id() != userdetail_id) {
			result.setServiceResult(false);
			result.setResultInfo("不能删除他人的提问");
		}
		else{
			questionService.deleteQuestion(question);
			result.setResultInfo("删除成功");
		}
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	/**
	 * 发表回答
	 */
	@RequestMapping(value = "/saveAnswer", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> saveAnswer(Answer answer,Integer userdetail_id){
		result = new ResultMessage();
		result.setServiceResult(true);
		Question question = questionService.findOneQuestion(answer.getQuestion_id());
		if(userdetail_id==question.getUserDetail_id()){
			result.setServiceResult(false);
			result.setResultInfo("回复失败");
		}
		else{
			Integer id = questionService.saveAnswer(answer);
			Answer answer_vo = questionService.getOneAnswer(id);
			UserDetail ud = userDetailService.getUserInfo(answer_vo.getUserDetail_id());
			Answer_User au = getAnswer_user(ud,answer_vo);
			result.getResultParm().put("answer_user", au);
			result.setResultInfo("回复成功");
		}
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	/**
	 * 删除回答
	 * @param answer_id
	 * @param userdetail_id
	 * @return
	 */
	@RequestMapping(value = "/deleteAnswer", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> deleteAnswer(Integer answer_id,Integer userdetail_id){
		result = new ResultMessage();
		result.setServiceResult(true);
		Answer answer = questionService.getOneAnswer(answer_id);
		Question question = questionService.findOneQuestion(answer.getQuestion_id());
		if(answer.getUserDetail_id()!=userdetail_id||userdetail_id!=question.getUserDetail_id()){
			result.setResultInfo("没有权限删除此回复");
			result.setServiceResult(false);
		}
		else{
			questionService.deleteAnswer(answer,question);
			result.setResultInfo("删除成功");
		}
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	/**
	 * 获取某问题的所有答案
	 * @param pageInfo
	 * @param question_id
	 * @return
	 */
	@RequestMapping(value = "/getAnswer", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> getAnswer(PageInfo pageInfo,Integer question_id){
		result = new ResultMessage();
		result.setServiceResult(true);
		List<Answer> answer_list = questionService.getAnswer(pageInfo,question_id);
		Question question = questionService.findOneQuestion(question_id);
		//移除最佳答案
		if(question.getAccept()!=0){
			Answer answer_vo = questionService.getOneAnswer(question.getAccept());
			answer_list.remove(answer_vo);
		}
		List<Answer_User> Answer_user_list = getAnswer_user_list(answer_list);
		result.getResultParm().put("answer_user_list", Answer_user_list);
		result.getResultParm().put("total_count", pageInfo.getTotalCount());
		result.setResultInfo("获取所有答案完成");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	
	
	@RequestMapping(value = "/saveBestAnswer", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> saveBestAnswer(Integer answer_id,Integer question_id){
		result = new ResultMessage();
		result.setServiceResult(true);
		Question question = questionService.findOneQuestion(question_id);
		if(question.getAccept()!=0){
			result.setResultInfo("已经存在采纳答案");
			result.setServiceResult(false);
		}
		else{
			Answer answer = questionService.getOneAnswer(answer_id);
			UserDetail ud = userDetailService.getUserInfo(answer.getUserDetail_id());
			questionService.saveBestAnswer(answer_id,question_id);
			result.setResultInfo("采纳成功");
			Answer_User au = getAnswer_user(ud,answer);
			result.getResultParm().put("answer_user", au);
		}
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	/**
	 * 得到最佳答案
	 * @param answer_id
	 * @return
	 */
	@RequestMapping(value = "/getBestAnswer", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> getBestAnswer(Integer question_id){
		result = new ResultMessage();
		result.setServiceResult(true);
		Question question = questionService.findOneQuestion(question_id);
		//获取最佳答案
		Answer answer = questionService.getOneAnswer(question.getAccept());
		UserDetail ud = userDetailService.getUserInfo(answer.getUserDetail_id());
		Answer_User au = getAnswer_user(ud,answer);
		result.getResultParm().put("answer_user", au);
		result.setResultInfo("获取成功");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	/**
	 * 分页查询所有问题-采纳排序
	 */
	@RequestMapping(value = "/getQuestionByAccept", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> getQuestionByAccept(PageInfo pageInfo,Integer is_accept) {
		result = new ResultMessage();
		result.setServiceResult(true);
		List<Question_User> question_user_list = null;
		question_user_list = questionService.findQuestionByAccept(pageInfo,is_accept);
		if (question_user_list.size() <= 0) {
			result.setServiceResult(false);
			result.setResultInfo("没有更多了");
		} else {
			result.setResultInfo("问题获取成功");
		}
		result.getResultParm().put("question_user", question_user_list);
		result.getResultParm().put("total_count", pageInfo.getTotalCount());
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	/**
	 * 分页查询所有问题-回答量排序
	 */
	@RequestMapping(value = "/getQuestionByAnswer", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> getQuestionByAnswer(PageInfo pageInfo) {
		result = new ResultMessage();
		result.setServiceResult(true);
		List<Question_User> question_user_list = null;
		question_user_list = questionService.getQuestionByAnswer(pageInfo);
		if (question_user_list.size() <= 0) {
			result.setServiceResult(false);
			result.setResultInfo("没有更多了");
		} else {
			result.setResultInfo("问题获取成功");
		}
		result.getResultParm().put("question_user", question_user_list);
		result.getResultParm().put("total_count", pageInfo.getTotalCount());
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	/**
	 * 分页查询所有问题-阅读量排序
	 */
	@RequestMapping(value = "/getQuestionByRead", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> getQuestionByRead(PageInfo pageInfo) {
		result = new ResultMessage();
		result.setServiceResult(true);
		List<Question_User> question_user_list = null;
		question_user_list = questionService.getQuestionByRead(pageInfo);
		if (question_user_list.size() <= 0) {
			result.setServiceResult(false);
			result.setResultInfo("没有更多了");
		} else {
			result.setResultInfo("问题获取成功");
		}
		result.getResultParm().put("question_user", question_user_list);
		result.getResultParm().put("total_count", pageInfo.getTotalCount());
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	/**
	 * 搜索
	 */
	@RequestMapping(value = "/SearchQuestion", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> SearchQuestion(PageInfo pageInfo,String search) {
		result = new ResultMessage();
		result.setServiceResult(true);
		List<Question_User> question_user_list = null;
		question_user_list = questionService.SearchQuestion(pageInfo,search);
		if (question_user_list.size() <= 0) {
			result.setServiceResult(false);
			result.setResultInfo("没有更多了");
		} else {
			result.setResultInfo("问题获取成功");
		}
		result.getResultParm().put("question_user", question_user_list);
		result.getResultParm().put("total_count", pageInfo.getTotalCount());
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	
	private Answer_User getAnswer_user(UserDetail ud, Answer answer) {
		return new Answer_User(ud.getName(), ud.getHead(), answer.getId(),
				answer.getContent(), answer.getUserDetail_id(),
				answer.getQuestion_id(), answer.getCreatetime());
	}
	
	private List<Answer_User> getAnswer_user_list(List<Answer> answer_list){
		List<Answer_User> Answer_user_list = new ArrayList<Answer_User>();
		for(Answer answer:answer_list){
			UserDetail ud = userDetailService.getUserInfo(answer.getUserDetail_id());
			Answer_User au = getAnswer_user(ud,answer);
			Answer_user_list.add(au);
		}
		return Answer_user_list;
	}
}
